package com.tomatosystem.baby;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class EoBoardDao extends DataBase {

	public EoBoardDao() {
		super();
	}
	
	public boolean insertEoBoard(EoBoardDto dto){
		String sql="INSERT INTO EOBOARD "
				  +"( SEQ,"
				  +" WRITER,"
				  +" TITLE,"
				  +" CONTS,"
				  +" REGDATE)"
				  +" VALUES( EOBOARD_SEQ.NEXTVAL,?,?,?,SYSDATE)";
		Connection conn=null;
		PreparedStatement psmt = null;
		int count=0;
		
		try {
			conn=this.getConnection();
			log("2/6");
			psmt=conn.prepareStatement(sql);
			int i = 1;
			psmt.setString(i++, dto.getWriter());
			psmt.setString(i++, dto.getTitle());
			psmt.setString(i++, dto.getConts());
			log("3/6");
			count=psmt.executeUpdate();
			log("4/6");
		} catch (SQLException e) {
			log(this.getClass()+": insertEoBoard :" ,e);
		}finally{
			this.close(conn, psmt, null);
		}
		return count>0?true:false;

	}

	
	public EoBoardDto getEoBoard(int seq){
		EoBoardDto dto=new EoBoardDto();
		String sql=" SELECT SEQ, WRITER, TITLE, CONTS, REGDATE "
			      +" FROM EOBOARD WHERE SEQ=? ";
		Connection conn=null;
		PreparedStatement psmt=null;
		ResultSet rs=null;
		
		try {
			conn=this.getConnection();
			log("2/6");
			psmt=conn.prepareStatement(sql);
			psmt.setInt(1, seq);
			log("3/6");
			rs=psmt.executeQuery();
			log("4/6");
			while(rs.next()){
				int i=1;
				dto.setSeq(rs.getInt(i++));
				dto.setWriter(rs.getString(i++));
				dto.setTitle(rs.getString(i++));
				dto.setConts(rs.getString(i++));
				dto.setRegdate(rs.getDate(i++));
			}
		} catch (SQLException e) {
			log(this.getClass()+": insertEoBoard : " ,e);
		}finally{
			this.close(conn, psmt, rs);
		}
		return dto;
	}
	
	public boolean updateEoBoard(EoBoardDto dto){
		String sql=" UPDATE EOBOARD SET "
			      +" WRITER=?,"
			      +" TITLE=?,"
			      +" CONTS=?,"
			      +" REGDATE=SYSDATE "
			      +" WHERE SEQ=?";
				
		Connection conn=null;
		PreparedStatement psmt=null;
		int count=0;
		try {
			conn=this.getConnection();
			log("2/6");
			psmt=conn.prepareStatement(sql);
			int i=1;
			psmt.setString(i++, dto.getWriter());
			psmt.setString(i++, dto.getTitle());
			psmt.setString(i++, dto.getConts());
			psmt.setInt(i++, dto.getSeq());
			log("3/6");
			count=psmt.executeUpdate();
			log("4/6");
		} catch (SQLException e) {
			log(this.getClass()+": insertEoBoard : " ,e);
		}finally{
			this.close(conn, psmt, null);
		}
		return count>0?true:false;
	}
	
	public boolean deleteEoBoard(int seq){
		String sql=" DELETE FROM EOBOARD "
			      +" WHERE SEQ=?";
			      				
		Connection conn=null;
		PreparedStatement psmt=null;
		int count=0;
		
		try {
			conn=this.getConnection();
			log("2/6");
			psmt=conn.prepareStatement(sql);
			psmt.setInt(1, seq);
			log("3/6");
			count=psmt.executeUpdate();
			log("4/6");
			
		} catch (SQLException e) {
			log(this.getClass()+": insertEoBoard : " ,e);
		}finally{
			this.close(conn, psmt, null);
		}
		return count>0?true:false;
	}
	
	public ArrayList<EoBoardDto> getAllEoBoard(){
		ArrayList<EoBoardDto> eos = new ArrayList<EoBoardDto>();
		
		String sql=" SELECT SEQ, WRITER, TITLE, CONTS, REGDATE "
			      +" FROM EOBOARD";
		Connection conn=null;
		PreparedStatement psmt=null;
		ResultSet rs=null;
		
		try {
			conn=this.getConnection();
			log("2/6");
			psmt=conn.prepareStatement(sql);
			log("3/6");
			rs=psmt.executeQuery();
			log("4/6");
			while(rs.next()){
				int i=1;
				EoBoardDto dto=new EoBoardDto();
				dto.setSeq(rs.getInt(i++));
				dto.setWriter(rs.getString(i++));
				dto.setTitle(rs.getString(i++));
				dto.setConts(rs.getString(i++));
				dto.setRegdate(rs.getDate(i++));
				eos.add(dto);
			}
		} catch (SQLException e) {
			log(this.getClass()+": insertEoBoard : " ,e);
		}finally{
			this.close(conn, psmt, rs);
		}
		return eos;
	}
}
